/* 
 * formatCurrency function from Stackoverflow accredited to Patrick Desjardins
 * 
 * @param{number} decimals: decimal value to format
 * @param{String} decimal_sep: character used as deciaml separtor, it defaults to '.' when omitted
 * @param{String} thousands_sep: char used as thousands separator, it defaults to ',' when omitted
*/
/*
Number.prototype.formatMoney(decimals, decimal_sep, thousands_sep) {
	var n = this;
	var c = isNaN(decimals) ? 2 : Math.abs(decimals); //if decimal is zero we must take it, it means user does not want to show any decimal
	var d = decimal_sep || '.'; //if no decimal separator is passed we use the dot as default decimal separator (we MUST use a decimal separator)
*/
   /*
   according to [http://stackoverflow.com/questions/411352/how-best-to-determine-if-an-argument-is-not-sent-to-the-javascript-function]
   the fastest way to check for not defined parameter is to use typeof value === 'undefined' 
   rather than doing value === undefined.
   */
/*
   var t = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep; //if you don't want to use a thousands separator you can pass empty string as thousands_sep value

   var sign = (n < 0) ? '-' : '';

   //extracting the absolute value of the integer part of the number and converting to string
   var i = parseInt(n = Math.abs(n).toFixed(c)) + ''; 

   var j = ((j = i.length) > 3) ? j % 3 : 0;
   
   return sign + (j ? i.substr(0, j) + t : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : ''); 
}
*/

/* Original code from Patrick which is way sexier in my opinion */
Number.prototype.formatMoney = function(c, d, t){
var n = this, 
    c = isNaN(c = Math.abs(c)) ? 2 : c, 
    d = d == undefined ? "." : d, 
    t = t == undefined ? "," : t, 
    s = n < 0 ? "-" : "", 
    i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", 
    j = (j = i.length) > 3 ? j % 3 : 0;
   return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
 };
